/**
 * @Description: 本文件提供系统级别的工具
 * @author Fengli
 * @date 4/29/19
 */

/**
 * 生成UUID
 * @returns {string}
 */
export function getUUID () {
  let d = new Date().getTime()
  let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    let r = (d + Math.random() * 16) % 16 | 0
    d = Math.floor(d / 16)
    return (c === 'x' ? r : (r & 0x7 | 0x8)).toString(16)
  })
  return uuid.replace(/-/g, '')
}

/**
 * 获取某一年的某一个周的开始日期
 * @param year
 * @param week
 * @returns {string}
 */
export function getWeekStartDate (year, week) {
  // Thursday in current week decides the year.
  const date = new Date(year, 0, 4)
  date.setDate(date.getDate() - date.getDay())
  date.setTime(date.getTime() + 7 * 86400000 * (week - 1))
  return date
}

/**
 * 获取某一年的某一个周的结束日期
 * @param year
 * @param week
 * @returns {string}
 */
export function getWeekEndDate (year, week) {
  // Thursday in current week decides the year.
  const date = new Date(year, 0, 4)
  date.setDate(date.getDate() - date.getDay())
  date.setTime(date.getTime() + (7 * 86400000 * week) - 1)
  return date
}

/**
 * 获取某一年的某一个周的开始日期和结束日期
 * @param year
 * @param week
 * @returns {string}
 */
export function getWeekDate (year, week) {
  return getWeekStartDate(year, week) + '-' + getWeekEndDate(year, week)
}

// 以下几个函数是判断当前日期所对应的周数，如2017-1-1，为2017年第一周,return 1
/**
 2  * 判断年份是否为润年
 3  *
 4  * @param {Number} year
 5  */
function isLeapYear (year) {
  return (year % 400 === 0) || (year % 4 === 0 && year % 100 !== 0)
}
/**
 10  * 获取某一年份的某一月份的天数
 11  *
 12  * @param {Number} year
 13  * @param {Number} month
 14  */
function getMonthDays (year, month) {
  return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28)
}
/**
 27  * 获取某年的某天是第几周
 28  * @param {Number} y
 29  * @param {Number} m
 30  * @param {Number} d
 31  * @returns {Number}
 32  */
export function getWeekNumber (day) {
  let now = day
  let year = now.getFullYear()
  let month = now.getMonth()
  let days = now.getDate()
  // 那一天是那一年中的第多少天
  for (var i = 0; i < month; i++) {
    days += getMonthDays(year, i)
  }

  // 那一年第一天是星期几
  let yearFirstDay = new Date(year, 0, 1).getDay() || 7

  let week = null
  if (yearFirstDay === 1) {
    week = Math.ceil(days / yearFirstDay)
  } else {
    days -= (7 - yearFirstDay + 1)
    week = Math.ceil(days / 7) + 1
  }

  return week
}

/* 日期格式化 */
export function formateDate () {
  Date.prototype.format = function (fmt) {
    var o = {
      'M+': this.getMonth() + 1, // 月份
      'd+': this.getDate(), // 日
      'h+': this.getHours(), // 小时
      'm+': this.getMinutes(), // 分
      's+': this.getSeconds(), // 秒
      'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
      'S': this.getMilliseconds() // 毫秒
    }
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (var k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
      }
    }
    return fmt
  }
}
